//
//  RadarRemindView.m
//  BrushFace
//
//  Created by Harry on 16/1/29.
//  Copyright © 2016年 Zzz. All rights reserved.
//

#import "RadarRemindView.h"

@interface RadarRemindView ()
@property(nonatomic,strong)NSTimer *timer;

@end
@implementation RadarRemindView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _friendLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 25)];
        [_friendLable setTextColor:[UIColor whiteColor]];
        [_friendLable setFont:[UIFont systemFontOfSize:13]];
        [_friendLable setTextAlignment:NSTextAlignmentRight];
        _timerlable =[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_friendLable.frame), 0, SCREEN_WIDTH-150, 25)];
        [_timerlable setTextColor:[UIColor whiteColor]];
        [_timerlable setFont:[UIFont systemFontOfSize:13]];
        _bottomLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 25, SCREEN_WIDTH, 25)];
        [_bottomLable setTextAlignment:NSTextAlignmentCenter];
        [_bottomLable setTextColor:[UIColor whiteColor]];
        [_bottomLable setFont:[UIFont systemFontOfSize:13]];
        [self addSubview:_friendLable];
        [self addSubview:_timerlable];
        [self addSubview:_bottomLable];
        self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.2];
    }
    return self;
}
-(void)layoutSubviews
{
  _bottomLable.text = @"好友出借后会通过短信通知您哒";
  
}
-(void)timerStart
{
    if(!_timer)
    {
        _timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeChanged) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
        [_timer fire];
       
        
    }
}
-(void)timeChanged
{
    
    _remainsecond = _remainsecond-1;
    _timerlable.text = [self timeFormatted:_remainsecond];
    
    if (_remainsecond <= 0)
    {
        
        _timerlable.text = @"剩余时间:『已超时』";


        [self timerStop];
    }
}
-(void)timerStop
{
    if (_timer) {
      
        
        //如果定时器在运行
        if ([self.timer isValid]) {
            
            NSLog(@"单击停止按钮，取消定时器！！");
            
            [self.timer invalidate];
            
            _timer=nil;
            
        }
        
    }
}
-(void)setRemainsecond:(NSInteger)remainsecond
{
    _remainsecond = remainsecond;
    _timerlable.text = [self timeFormatted:_remainsecond ];
    if (_remainsecond>0)
    {
         [self timerStart];
    }
    else
    {
    _timerlable.text = @"剩余时间:『已超时』";
    }
   
}


- (NSString *)timeFormatted:(NSInteger)totalSeconds
{
    if (totalSeconds>=24*60*60)
    {
        NSInteger otherTotalSeconds = totalSeconds -24*60*60;
        NSInteger seconds = otherTotalSeconds % 60;
        NSInteger minutes = (otherTotalSeconds / 60) % 60;
        NSInteger hours = otherTotalSeconds / 3600;
        return [NSString stringWithFormat:@"剩余时间: 1天:%02ld:%02ld:%02ld",hours, minutes, seconds];
    }
    else
    {
        NSInteger seconds = totalSeconds % 60;
        NSInteger minutes = (totalSeconds / 60) % 60;
        NSInteger hours = totalSeconds / 3600;
        
        return [NSString stringWithFormat:@"剩余时间: 0天:%02ld:%02ld:%02ld",hours, minutes, seconds];

    }
    }
@end
